#!/usr/bin/env bash
# doinstall:
# Install U++ binaries and sources
# 'sh doinstall --help' will show you all available options

### Sourced file(s)

. ./function_library

### Variables

project_name="upp"
package_name="$project_name"

show_debug_info="true"
show_debug_warning="true"
show_debug_error="true"

installdoc="false"

### Constants

EXIT_SUCCESS=0
EXIT_ERROR_COMMAND_LINE=1

SCRIPT_NAME=$(basename "$0" | tr '[:lower:]' '[:upper:]' )

### Function declarations

show_usage()
{
# current: Calling directly: ./doinstall --prefix=${prefix} --bindir=${bindir} --datadir=${datadir} --mandir=${mandir} --docdir=${docdir} --verbose
# current: Using Makefile:   make install prefix=${prefix} exec-prefix=${exec_prefix} bindir=${bindir} datadir=${datadir} mandir=${mandir} docdir=${docdir}

# full: Using Makefile: make install prefix=${prefix} exec-prefix=${exec_prefix} bindir=${bindir} sbindir=${sbindir} sysconfdir=${sysconfdir} datadir=${datadir} includedir=${includedir} libdir=${libdir} libexecdir=${libexecdir} localstatedir=${localstatedir} sharedstatedir=${sharedstatedir} mandir=${mandir} infodir=${infodir} docdir=${docdir}
# full: ./doinstall --prefix=${prefix} --exec-prefix=${exec_prefix} --bindir=${bindir} --sbindir=${sbindir} --sysconfdir=${sysconfdir} --datadir=${datadir} --includedir=${includedir} --libdir=${libdir} --libexecdir=${libexecdir} --localstatedir=${localstatedir} --sharedstatedir=${sharedstatedir} --mandir=${mandir} --infodir=${infodir} --docdir=${docdir}

    echo "Usage: ./doinstall [options]"
    echo
    echo 'Options:'
    echo '  DESTDIR=<path>          : installation root directory (prepending each target directory to provides staged installs ( default: void )' 
##  echo '  --prefix=<path>         : installation prefix ( default: /usr/local )'
    echo '  --prefix=<path>         : installation prefix ( default: $HOME )'
    echo '  --exec-prefix=<path>    : installation exec prefix for executables and subroutine ( default: /usr/local e.q $(prefix) )'
    echo '  --bindir=<path>         : programs installation directory, executable that users can run ( default: /usr/local/bin e.g. $(exec_prefix)/bin )'
#   echo '  --sbindir=<path>        : programs installation directory, executable for system administrators ( default: /usr/local/sbin e.g. $(exec_prefix)/sbin )'
#   echo '  --sysconfdir=<path>     : read-only data files directory, for configuring a host ( default: /usr/local/etc e.g. $(prefix)/etc )'
#   echo '  --datarootdir=<path>    : read-only architecture-independent data files directory ( default: /usr/local/share e.g $(prefix)/share )'
##  echo '  --datadir=<path>        : read-only idiosyncratic architecture-independent data files directory ( default: /usr/local/share e.g $(datarootdir) )'
    echo '  --datadir=<path>        : read-only idiosyncratic architecture-independent data files directory ( default: /usr/local/share e.g $(prefix)/share )'
#   echo '  --includedir=<path>     : header files installation directory, headers to be included by user programs ( default: /usr/local/include e.g $(prefix)/include ).'
#   echo '  --libdir=<path>         : libraries and object files directory ( default: /usr/local/lib e.g $(exec_prefix)/lib )'
#   echo '  --libexecdir=<path>     : programs installation directory, executable run by applications and not by users ( default: /usr/local/libexec e.g. $(exec_prefix)/libexec )'
#   echo '  --localstatedir=<path>  : architecture-independent data installation directory, data that programs can modify while they run ( default: /usr/local/var e.g. $(prefix)/var )'
#   echo '  --sharedstatedir=<path> : data files directory where programs modify their files at runtime ( default: /usr/local/var/lib e.g. $localstatedir/lib )'
#   echo '  --sharedstatedir=<path> : data files directory where programs modify their files at runtime ( default: $prefix/'$package_name' )'
##  echo '  --mandir=<path>         : man pages top level directory ( default: /usr/local/share/man e.g. $(datarootdir)/share/man )'
    echo '  --mandir=<path>         : man pages top level directory ( default: /usr/local/share/man e.g. $(datadir)/share/man )'
    echo '  --docdir=<path>         : documentation files directory ( default: /usr/local/share/doc e.g. $(datatdir)/doc )'
#   echo '  --infodir=<path>        : info files directory ( default: /usr/local/share/info e.g. $(datarootdir)/info )'
#   echo '  --enable-debug          : include debug symbols'
#   echo '  --disable-debug         : do not include debug symbols'
    echo '  --installdoc=FLAGS      : install documentation in docdir ( default: false )'
    echo '  --verbose               : increase verbosity'
    echo '  --info=FLAGS            : fine-grained informational verbosity ( default: true )'
    echo '  --warning=FLAGS         : fine-grained warning verbosity ( default: true )'
    echo '  --debug=FLAGS           : fine-grained debug verbosity ( default: true )'
    echo '  --help                  : show current usage information'
    echo
    echo 'Note:'
    echo " - If you don't set prefix, installation will be done in your home directory in an U++ style (other options will be ignored)."
    echo " - doinstall uses environment variables. If 'prefix' is different than HOME, 'bindir', 'datadir' and other environment variables will be used."
    echo " - All option parameters like --prefix, --bindir, ..., will superseed environment variables."
    echo
}


# log_debug_info "Processing parameters"

for arg in "$@"
do
  case "$arg" in

  DESTDIR=*)
    DESTDIR=$( echo $arg | sed 's/DESTDIR=//' )
    ;;
  
  --prefix=*)
    prefix=$( echo $arg | sed 's/--prefix=//' )
    ;;

  --exec-prefix=*)
    exec_prefix=$( echo $arg | sed 's/--exec-prefix=//' )
    ;;

  --bindir=*)
    bindir=$( echo $arg | sed 's/--bindir=//' )
    ;;

#  --sbindir=*)
#    sbindir=$( echo $arg | sed 's/--sbindir=//' )
#    ;;

#  --sysconfdir=*)
#    sysconfdir=$( echo $arg | sed 's/--sysconfdir=//' )
#    ;;

#  --datarootdir=*)
#    datarootdir=$( echo $arg | sed 's/--datarootdir=//' )
#    ;;

  --datadir=*)
    datadir=$( echo $arg | sed 's/--datadir=//' )
    ;;

#  --includedir=*)
#    includedir=$( echo $arg | sed 's/--includedir=//' )
#    ;;

#  --libdir=*)
#    libdir=$( echo $arg | sed 's/--libdir=//' )
#    ;;

#  --libexecdir=*)
#    libexecdir=$( echo $arg | sed 's/--libexecdir=//' )
#    ;;

#  --localstatedir=*)
#    localstatedir=$( echo $arg | sed 's/--localstatedir=//' )
#    ;;

#  --sharedstatedir=*)
#    sharedstatedir=$( echo $arg | sed 's/--sharedstatedir=//' )
#    ;;

  --mandir=*)
    mandir=$( echo $arg | sed 's/--mandir=//' )
    ;;

  --docdir=*)
    docdir=$( echo $arg | sed 's/--docdir=//' )
    ;;

#  --infodir=*)
#    infodir=$( echo $arg | sed 's/--infodir=//' )
#    ;;

#  --enable-debug)
#    debugsym=true
#    ;;

#  --disable-debug)
#    debugsym=true
#    ;;

  --installdoc=*)
    installdoc=$( echo $arg | sed 's/--installdoc=//' )
    if [ "$installdoc" != "true" -a "$installdoc" != "false"  ]
    then
      show_usage
      exit $EXIT_ERROR_COMMAND_LINE
    fi
    ;;

  --verbose)
    show_debug_info=true
    show_debug_warning=true
    show_debug_error=true
    ;;
    
  --info=*)
    show_debug_info=$( echo $arg | sed 's/--info=//' )
    if [ "$show_debug_info" != "true" -a "$show_debug_info" != "false"  ]
    then
      show_usage
      exit $EXIT_ERROR_COMMAND_LINE
    fi
    ;;
    
  --warning=*)
    show_debug_warning=$( echo $arg | sed 's/--warning=//' )
    if [ "$show_debug_warning" != "true" -a "$show_debug_warning" != "false"  ]
    then
      show_usage
      exit $EXIT_ERROR_COMMAND_LINE
    fi
    ;;
    
  --error=*)
    show_debug_error=$( echo $arg | sed 's/--error=//' )
    if [ "$show_debug_error" != "true" -a "$show_debug_error" != "false"  ]
    then
      show_usage
      exit $EXIT_ERROR_COMMAND_LINE
    fi
    ;;

  --help)
    show_usage
    exit $EXIT_SUCCESS
    ;;
    
    *)
    log_debug_error "Bad command line argument '$arg'"
    show_usage
    exit $EXIT_ERROR_COMMAND_LINE
    
    esac

done

[ -z "$DESTDIR"        ] && DESTDIR=""					# DESTDIR=""
[ -z "$prefix"         ] && prefix="$HOME"				# prefix="/usr/local"
[ -z "$exec_prefix"    ] && exec_prefix="$prefix"			# exec_prefix="$prefix"
[ -z "$bindir"         ] && bindir="$exec_prefix/bin"			# bindir="$exec_prefix/bin"
#[ -z "$sbindir"        ] && sbindir="$exec_prefix/sbin"		# sbindir="$exec_prefix/sbin"
#[ -z "$sysconfdir"     ] && sysconfdir="/etc"				# sysconfdir="/etc"
#[ -z "$datarootdir"    ] && datarootdir="$prefix/share"		# datarootdir="$prefix/share"
[ -z "$datadir"        ] && datadir="$prefix/share"			# datadir="$datarootdir"
#[ -z "$includedir"     ] && includedir="$prefix/include"		# includedir="$prefix/include"
#[ -z "$libdir"         ] && libdir="$prefix/lib"			# libdir="$prefix/lib"
#[ -z "$libexecdir"     ] && libexecdir="$exec_prefix/libexec"		# libexecdir="$exec_prefix/libexec"
#[ -z "$localstatedir"  ] && localstatedir="/var"			# localstatedir="/var"
#[ -z "$sharedstatedir" ] && sharedstatedir="$localstatedir/lib"	# sharedstatedir="$localstatedir/lib"
[ -z "$docdir"         ] && docdir="$datadir/doc"			# docdir="$datadir/doc"
[ -z "$mandir"         ] && mandir="$datadir/man"			# mandir="$datadir/man"
#[ -z "$infodir"        ] && infodir="$datadir/info"			# infodir="$datadir/info"


### Main script start here

if [ ! -z "$DESTDIR" ]
then
  if [ "${prefix:0:1}" != "/" ]
  then
    if [ "${DESTDIR:${#DESTDIR}-1}" != "/" ]
    then
      DESTDIR="$DESTDIR/"
    fi
  else
    while [ "${DESTDIR:${#DESTDIR}-1}" = "/" ]
    do
      DESTDIR="${DESTDIR:0:-1}"
    done
  fi
fi

if [ "$prefix" = "$HOME" ]
then

log_debug_info ">> Starting installation in HOME directory (U++ style)"
log_debug_info "Variable values:"
log_debug_info "DESTDIR         = $DESTDIR"
log_debug_info "prefix          = $prefix"
#log_debug_info "exec_prefix     = $exec_prefix"
#log_debug_info "bindir          = $bindir"
#log_debug_info "sbindir         = $sbindir"
#log_debug_info "sysconfdir      = $sysconfdir"
#log_debug_info "datarootdir     = $datarootdir"
#log_debug_info "datadir         = $datadir"
#log_debug_info "includedir      = $includedir"
#log_debug_info "libdir          = $libdir"
#log_debug_info "libexecdir      = $libexecdir"
#log_debug_info "localstatedir   = $localstatedir"
#log_debug_info "sharedstatedir  = $sharedstatedir"
#log_debug_info "docdir          = $docdir"
#log_debug_info "mandir          = $mandir"
#log_debug_info "infodir         = $infodir"


log_debug_info "Creating upp, upp.out, MyApps and .upp directories in $DESTDIR$prefix/"
  install -d "$DESTDIR$prefix/upp"
  install -d "$DESTDIR$prefix/upp.out"
  install -d "$DESTDIR$prefix/MyApps"
  install -d "$DESTDIR$prefix/.upp/theide"
  install -d "$DESTDIR$prefix/.upp/theide/speller"

log_debug_info "Copying theide and umk"
  install ./theide "$DESTDIR$prefix/upp/theide"
  install ./umk    "$DESTDIR$prefix/upp/umk"

log_debug_info "Creating theide.desktop, copying default icon and creating user menu entry"

cat > "theide.desktop" << EOF
[Desktop Entry]
Encoding=UTF-8
Name=TheIDE
GenericName=TheIDE
Comment=IDE for cross-platform C++ development 
MimeType=application/x-upp;
Exec=$DESTDIR$prefix/upp/theide
Icon=$DESTDIR$prefix/upp/theide.png
Terminal=false
Type=Application
Categories=Development;IDE;
StartupNotify=false
EOF

  chmod +x "theide.desktop"

  cp -p "uppsrc/ide/theide-48.png" "theide.png"

  cp -p "theide.desktop" "$DESTDIR$prefix/upp/"
  cp -p "theide.png" "$DESTDIR$prefix/upp/"
  cp -p "do-xdg-desktop-menu" "$DESTDIR$prefix/upp/"
  cp -p "douninstall" "$DESTDIR$prefix/upp/uninstall"

  ./do-xdg-desktop-menu install

log_debug_info "Copying U++ source code"
  cp -r uppsrc    "$DESTDIR$prefix/upp"
  cp -r examples  "$DESTDIR$prefix/upp"
  cp -r bazaar    "$DESTDIR$prefix/upp"
  cp -r tutorial  "$DESTDIR$prefix/upp"
  cp -r reference "$DESTDIR$prefix/upp"

#<begin> TODO: BINARY FROM COMPILATION AND build_info.h ARE STILL IN UPPSRC AND MUST NOT BE PART OF INSTALLATION. MUST USE A DIFFERENT APPROCHE THAN REMOVING THEM AFTER INSTALL (LIKE DEFINING UPPOUT ELSEWHERE THAN IN uppsrc/_out)
log_debug_info "Cleaning source code"
  rm -r "$DESTDIR$prefix/upp/uppsrc/_out"
  rm -r "$DESTDIR$prefix/upp/uppsrc/ide.out"
  rm -r "$DESTDIR$prefix/upp/uppsrc/umk.out"
#<end> TODO


  if [ "$(ls *.udc 2>/dev/null)" != "" ]
  then
log_debug_info "Copying" *.udc
    cp -p *.udc "$DESTDIR$prefix/.upp/theide/speller/"
  fi

  if [ "$installdoc" = "true" ]
  then
log_debug_info "Copying readme.md and license"
    cp -p readme.md          "$DESTDIR$prefix/upp"
    cp -p uppsrc/ide/Copying "$DESTDIR$prefix/upp"
  fi
 
log_debug_info "Generating default theide var files in $DESTDIR$prefix/.upp/theide/"
  echo "UPP = \"$DESTDIR$prefix/upp/uppsrc\";OUTPUT = \"$DESTDIR$prefix/upp.out\";"                               > "$DESTDIR$prefix/.upp/theide/uppsrc.var"
  echo "UPP = \"$DESTDIR$prefix/upp/examples;$DESTDIR$prefix/upp/uppsrc\";OUTPUT = \"$DESTDIR$prefix/upp.out\";"  > "$DESTDIR$prefix/.upp/theide/examples.var"
  echo "UPP = \"$DESTDIR$prefix/upp/reference;$DESTDIR$prefix/upp/uppsrc\";OUTPUT = \"$DESTDIR$prefix/upp.out\";" > "$DESTDIR$prefix/.upp/theide/reference.var"
  echo "UPP = \"$DESTDIR$prefix/upp/tutorial;$DESTDIR$prefix/upp/uppsrc\";OUTPUT = \"$DESTDIR$prefix/upp.out\";"  > "$DESTDIR$prefix/.upp/theide/tutorial.var"
  echo "UPP = \"$DESTDIR$prefix/upp/bazaar;$DESTDIR$prefix/upp/uppsrc\";OUTPUT = \"$DESTDIR$prefix/upp.out\";"    > "$DESTDIR$prefix/.upp/theide/examples-bazaar.var"
  echo "UPP = \"$DESTDIR$prefix/MyApps;$DESTDIR$prefix/upp/uppsrc\";OUTPUT = \"$DESTDIR$prefix/upp.out\";"        > "$DESTDIR$prefix/.upp/theide/MyApps.var"
  echo "UPP = \"$DESTDIR$prefix/MyApps;$DESTDIR$prefix/upp/bazaar;$DESTDIR$prefix/upp/uppsrc\";OUTPUT = \"$DESTDIR$prefix/upp.out\";" > "$DESTDIR$prefix/.upp/theide/MyApps-bazaar.var"

else

log_debug_info ">> Starting standard installation for POSIX operating systems"
log_debug_info "Variable values:"
log_debug_info "DESTDIR         = $DESTDIR"
log_debug_info "prefix          = $prefix"
log_debug_info "exec_prefix     = $exec_prefix"
log_debug_info "bindir          = $bindir"
#log_debug_info "sbindir         = $sbindir"
#log_debug_info "sysconfdir      = $sysconfdir"
#log_debug_info "datarootdir     = $datarootdir"
log_debug_info "datadir         = $datadir"
#log_debug_info "includedir      = $includedir"
#log_debug_info "libdir          = $libdir"
#log_debug_info "libexecdir      = $libexecdir"
#log_debug_info "localstatedir   = $localstatedir"
#log_debug_info "sharedstatedir  = $sharedstatedir"
log_debug_info "docdir          = $docdir"
log_debug_info "mandir          = $mandir"
#log_debug_info "infodir         = $infodir"

log_debug_info "Creating directories"
  install -d "$DESTDIR$bindir"
  install -d "$DESTDIR$datadir/$package_name"
  install -d "$DESTDIR$datadir/$package_name/speller"
  install -d "$DESTDIR$datadir/applications"
  install -d "$DESTDIR$datadir/icons/hicolor/48x48/apps"
  install -d "$DESTDIR$datadir/pixmaps"
  install -d "$DESTDIR$mandir/man1"
  if [ "$installdoc" = "true" ]
  then
    install -d "$DESTDIR$datadir/licenses/$package_name"
    install -d "$DESTDIR$docdir/$package_name"
  fi

log_debug_info "Copying theide and umk"
  install ./theide "$DESTDIR$bindir/theide"
  install ./umk    "$DESTDIR$bindir/umk"

log_debug_info "Copying man pages"
  cp -p "uppsrc/ide/theide.1" "$DESTDIR$mandir/man1/"
  cp -p "uppsrc/umk/umk.1"    "$DESTDIR$mandir/man1/"

log_debug_info "Copying icons and desktop file"
  cp -p "uppsrc/ide/theide.desktop" "$DESTDIR$datadir/applications/theide.desktop"
  cp -p "uppsrc/ide/theide-48.png"  "$DESTDIR$datadir/icons/hicolor/48x48/apps/theide.png"
  cp -p "uppsrc/ide/theide-48.png"  "$DESTDIR$datadir/pixmaps/theide.png"

log_debug_info "Copying U++ source code"
  cp -a bazaar    "$DESTDIR$datadir/$package_name/"
# cp -a Common    "$DESTDIR$datadir/$package_name/"
  cp -a examples  "$DESTDIR$datadir/$package_name/"
  cp -a reference "$DESTDIR$datadir/$package_name/"
  cp -a tutorial  "$DESTDIR$datadir/$package_name/"
  cp -a uppsrc    "$DESTDIR$datadir/$package_name/"
  
#<begin> TODO: BINARY FROM COMPILATION AND build_info.h ARE STILL IN UPPSRC AND MUST NOT BE PART OF INSTALLATION. MUST USE A DIFFERENT APPROCHE THAN REMOVING THEM AFTER INSTALL (LIKE DEFINING UPPOUT ELSEWHERE THAN IN uppsrc/_out)
log_debug_info "Cleaning source code"
  rm -fr "$DESTDIR$datadir/$package_name/uppsrc/_out"
  rm -f  "$DESTDIR$datadir/$package_name/uppsrc/ide.out"
  rm -f  "$DESTDIR$datadir/$package_name/uppsrc/umk.out"
  rm -f  "$DESTDIR$datadir/$package_name/uppsrc/build_info.h"
#<end> TODO

  if [ "$(ls *.bm 2>/dev/null)" != "" ]
  then
log_debug_info "Copying" *.bm
    cp -p *.bm  "$DESTDIR$datadir/$package_name/"
  fi

  if [ "$(ls *.udc 2>/dev/null)" != "" ]
  then
log_debug_info "Copying" *.udc
    cp -p *.udc "$DESTDIR$datadir/$package_name/speller/"
  fi

#   if [ "$(ls *.scd 2>/dev/null)" != "" ]
#   then
# log_debug_info "Copying" *.scd
#     cp -p *.scd "$DESTDIR$datadir/$package_name/speller/"
#   fi

  if [ "$installdoc" = "true" ]
  then
log_debug_info "Copying readme.md and license"
    cp -p readme.md          "$DESTDIR$docdir/$package_name/"
    cp -p uppsrc/ide/Copying "$DESTDIR$datadir/licenses/$package_name/"
  fi

fi

log_debug_info "<< End of installation"

exit $EXIT_SUCCESS
